package config

import (
	"fmt"
	"os"

	"github.com/spf13/viper"
)

var (
	v         = viper.New()
	appConfig = NewAppConfig()

	MongoDBName = "portal"
)

type Server struct {
	// 接远端 nginx proxy_pass
	ProxyAddr string `mapstructure:"proxy_addr"`
	// 接透传客户端请求
	TunnelAddr string `mapstructure:"tunnel_addr"`
}

type AppConfig struct {
	Server Server `mapstructure:"server"`
}

func NewAppConfig() *AppConfig {
	return &AppConfig{}
}

func init() {
	v.AutomaticEnv()
	v.SetConfigType("toml")
	v.AddConfigPath("conf")

	v.SetConfigName("config")

	if err := v.ReadInConfig(); err != nil {
		fmt.Println("\033[5;31m", "阻断错误信息：load config failed，", err, "\033[0m")
		os.Exit(-1)
	}

	if err := v.Unmarshal(&appConfig); err != nil {
		fmt.Println("\033[5;31m", "阻断错误信息：load config failed，", err, "\033[0m")
		os.Exit(-1)
	}
}

func Config() *AppConfig {
	return appConfig
}
